package com.itheima.controller;


import com.alibaba.fastjson.JSON;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.pojo.Category;
import com.itheima.pojo.Pageparam;
import com.itheima.result.R;
import com.itheima.service.ICategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;


/**
 * <p>
 * 菜品及套餐分类 前端控制器
 * </p>
 *
 * @author ${author}
 * @since 2021-11-28
 */
@Slf4j
@RestController
@RequestMapping("/category")
public class CategoryController {
    @Autowired
    private ICategoryService sc;
@PostMapping
    public R<String> addCategory(@RequestBody Category ca) {
   int row =sc.add(ca);
    if (row>0) {
        return R.success("添加分类成功");

    }
    return R.error("添加分类失败");
}

    @GetMapping("/page/{param}")
    public R<Page> findAllCategory(@PathVariable String param) {

        Pageparam page = JSON.parseObject(param, Pageparam.class);
        Page result = sc.findAllCategory(page);
        return R.success(result);
    }
    @DeleteMapping("/{id}")
    public R<String> deleteCategory(@PathVariable Long id) {
        int row=sc.delete(id);
        if (row>0) {
            return R.success("删除分类成功");

        }
        return R.error("删除分类失败");
    }
    @PutMapping
    public R<String> updateCategory(@RequestBody Category ca) {
        log.info("修改分类信息：{}",ca);
        int row =sc.updateCategory(ca);
        if (row>0) {
            return R.success("修改分类成功");

        }
        return R.error("修改分类失败");
    }

    @GetMapping("/list")
    public R<List<Category>> getDishCategory(Integer type) {
    List<Category> list=sc.select(type);

        return R.success(list);
    }
}




